﻿using System.Linq;
using CB.Stock.Data.Yahoo;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using CB.Stock.Core;
using Microsoft.Practices.Unity;
using System.Collections.Generic;

namespace Testing
{
    
    
    /// <summary>
    ///This is a test class for YahooStockDataServiceTest and is intended
    ///to contain all YahooStockDataServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class YahooStockDataServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetStockQueryId
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CB.Stock.Data.Yahoo.dll")]
        public void GetStockQueryIdTest()
        {
            IStockStaticInformation stock = new StockStaticInformation("Beijing SL Pharmaceutical Co",
                "002038", MarketType.ShenZhen); // TODO: Initialize to an appropriate value
            string expected = "002038.SZ"; // TODO: Initialize to an appropriate value
            string actual;
            actual = YahooStockDataService_Accessor.GetStockQueryId(stock);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetCurrentDataQueryParam
        ///</summary>
        [TestMethod()]
        [DeploymentItem("CB.Stock.Data.Yahoo.dll")]
        public void GetCurrentDataQueryParamTest()
        {
            StockDataType[] dataTypes = { StockDataType.SalePrice, StockDataType.PaidPrice, StockDataType.Symbol }; // TODO: Initialize to an appropriate value
            string expected = "p5p1s"; // TODO: Initialize to an appropriate value
            string actual;
            actual = YahooStockDataService_Accessor.GetCurrentDataQueryParam(dataTypes);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetCurrentData
        ///</summary>
        [TestMethod()]
        public void GetCurrentDataTest()
        {
            IUnityContainer container = new UnityContainer(); // TODO: Initialize to an appropriate value
            YahooStockDataService target = new YahooStockDataService(container);
                // TODO: Initialize to an appropriate value

            StockDataType[] dataTypes = new[]
                                            {
                                                StockDataType.PaidPrice,
                                                StockDataType.Symbol, StockDataType.LastTradePrice
                                            }; // TODO: Initialize to an appropriate value

            IStockStaticInformation[] stockInfors = {
                                                        new StockStaticInformation("Beijing SL Pharmaceutical Co",
                                                                                   "002038", MarketType.ShenZhen)
                                                    }; // TODO: Initialize to an appropriate value

            //IEnumerable<Tuple<IStockStaticInformation, IDictionary<StockDataType, IStockDynamicData>>> expected = null;// TODO: Initialize to an appropriate value

            IEnumerable<Tuple<IStockStaticInformation, IDictionary<StockDataType, IStockDynamicData>>> actual;
            actual = target.GetCurrentData(dataTypes, stockInfors);

            var actual1 = actual.ToArray();

            Assert.AreEqual(actual1.Length, stockInfors.Length, "Result count is not correct");
            for (int i = 0; i < stockInfors.Length; i++)
            {
                Assert.AreEqual(actual1[i].Item1, stockInfors[i], "Stock Information is not same");
                Assert.AreEqual(actual1[i].Item2.Keys.Count, 3, "Stock Types Count are not same");
                Assert.AreEqual(actual1[i].Item2[StockDataType.Symbol].Data, "002038.SZ",
                                "Stock Types Count are not same");
            }
        }

        /// <summary>
        ///A test for YahooStockDataService Constructor
        ///</summary>
        [TestMethod()]
        public void YahooStockDataServiceConstructorTest()
        {
            IUnityContainer container = new UnityContainer(); // TODO: Initialize to an appropriate value
            YahooStockDataService target = new YahooStockDataService(container);
            //Assert.Inconclusive("TODO: Implement code to verify target");
        }

        /// <summary>
        ///A test for GetHistoryData
        ///</summary>
        [TestMethod()]
        public void GetHistoryDataTest()
        {
            IUnityContainer container = new UnityContainer(); // TODO: Initialize to an appropriate value
            YahooStockDataService target = new YahooStockDataService(container); // TODO: Initialize to an appropriate value
            IStockStaticInformation stockInfo = new StockStaticInformation("Beijing SL Pharmaceutical Co",
                "002038", MarketType.ShenZhen); // TODO: Initialize to an appropriate value
            DateTime startDate = DateTime.Now.AddDays(-2); // TODO: Initialize to an appropriate value
            DateTime endDate = DateTime.Now; // TODO: Initialize to an appropriate value
            //IEnumerable<IDictionary<StockDataType, IStockDynamicData>> expected = null; // TODO: Initialize to an appropriate value
            var actual = target.GetHistoryData(stockInfo, startDate, endDate).ToArray();
            foreach (var actData in actual)
            {
                Assert.AreEqual(actData.Keys.Count, 5, "Get History date within 2 days fail");
            }
           
            startDate = new DateTime(2010,11,1);
            endDate = new DateTime(2010, 11, 3);
            actual = target.GetHistoryData(stockInfo, startDate, endDate).ToArray();
            Assert.AreEqual(actual.Length, 3, "Get History date in specified date fail");
            Assert.AreEqual(actual[0].Values.First().Time, startDate, "Get History date in specified date fail");
            Assert.AreEqual(actual[2].Values.First().Time, endDate, "Get History date in specified date fail");
            Assert.AreEqual(actual[1][StockDataType.Volume].Data, (Int64)2408900, "Get History date in specified date fail");
            Assert.AreEqual(actual[1][StockDataType.LastTradePrice].Data, 54.5000, "Get History date in specified date fail");
            Assert.AreEqual(actual[1][StockDataType.HighPrice].Data, 55.8200, "Get History date in specified date fail");
            Assert.AreEqual(actual[1][StockDataType.LowPrice].Data, 53.0000, "Get History date in specified date fail");
            Assert.AreEqual(actual[1][StockDataType.Open].Data, 55.8200, "Get History date in specified date fail");
        }
    }
}
